<?php
// +----------------------------------------------------------------------
// | OneKeyAdmin [ Believe that you can do better ]
// +----------------------------------------------------------------------
// | Copyright (c) 2020-2023 http://onekeyadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: MUKE <513038996@qq.com>
// +----------------------------------------------------------------------
namespace plugins\blog\api\controller;

use think\exception\ValidateException;
use app\api\BaseController;
use plugins\blog\api\validate\UserCollect as Validate;
use plugins\blog\api\model\Blog;
use plugins\blog\api\model\BlogCollect;
/**
 * 我的收藏
 */
class UserCollect extends BaseController
{
    /**
     * 用户权鉴
     */
    public $middleware = [\app\api\middleware\AuthCheck::class];
    
    /**
     * 显示资源列表
     */
    public function index()
    {
        if ($this->request->isPost()) {
            try {
                $input = input('post.');
                validate(Validate::class)->scene('index')->check($input);
            } catch ( ValidateException $e ) {
                return json(['status' => 'error', 'message' => $e->getError()]);
            }
            $where[] = ['user_id', '=', $this->request->userId];
            $order   = ['create_time' => 'desc'];
            $search  = ['date', 'keyword'];
            $count   = BlogCollect::where($where)->withSearch($search, $input)->count();
            $data    = BlogCollect::where($where)->withSearch($search, $input)->with(['blog'])->order($order)->page($input['page'], 10)->select();
            return json(['status' => 'success', 'message' => '获取成功', 'data' => $data, 'count' => $count]);
        }
    }

    /**
     * 保存更新的资源
     */
    public function save() 
    {
        if ($this->request->isPost()) {
            try {
                $input = input('post.');
                validate(Validate::class)->scene('save')->check($input);
            } catch ( ValidateException $e ) {
                return json(['status' => 'error', 'message' => $e->getError()]);
            }
            $exits = BlogCollect::where('user_id', $this->request->userId)->where('blog_id',$input['id'])->find(); 
            if ($exits) {
                return json(['status' => 'success', 'message' => '收藏成功']);
            }
            // 博客
            $blog = Blog::where('id',$input['id'])->find();
            // 新增
            BlogCollect::create([
                'user_id'     => $this->request->userId,
                'blog_id'     => $blog->id,
                'blog_cover'  => $blog->cover,
                'blog_title'  => $blog->title,
                'create_time' => date('Y-m-d H:i:s'),
            ]);
            return json(['status' => 'success', 'message' => '收藏成功']);
        }
    }
    
    /**
     * 删除指定资源
     */
    public function delete()
    {
        if ($this->request->isPost()) {
            try {
                $input = input('post.');
                validate(Validate::class)->scene('delete')->check($input);
            } catch ( ValidateException $e ) {
                return json(['status' => 'error', 'message' => $e->getError()]);
            }
            BlogCollect::where('blog_id', $input['id'])->where('user_id', $this->request->userId)->delete();
            return json(['status' => 'success','message' => '删除成功']);
        }
    }
}